package com.zy.openchat.core.model.group;

import android.os.Parcel;
import android.os.Parcelable;

import com.google.gson.annotations.SerializedName;
import com.zy.openchat.core.manager.TelegramManager;

public class Group_ChatMemberStatus implements Parcelable {
    //chatMemberStatusAdministrator, chatMemberStatusBanned, chatMemberStatusCreator, chatMemberStatusLeft, chatMemberStatusMember, and chatMemberStatusRestricted.
    @SerializedName("@type")
    public String type;

    //管理员 - chatMemberStatusAdministrator
    //A custom title of the administrator; 0-16 characters without emojis; applicable to supergroups only.
    public String custom_title;
    //True, if the current user can edit the administrator privileges for the called user.
    public boolean can_be_edited;
    //True, if the administrator can change the chat title, photo, and other settings.
    public boolean can_change_info;
    //True, if the administrator can create channel posts; applicable to channels only.
    public boolean can_post_messages;
    //True, if the administrator can edit messages of other users and pin messages; applicable to channels only.
    public boolean can_edit_messages;
    //True, if the administrator can delete messages of other users.
    public boolean can_delete_messages;
    //True, if the administrator can invite new users to the chat.
    public boolean can_invite_users;
    //True, if the administrator can restrict, ban, or unban chat members.
    public boolean can_restrict_members;
    //True, if the administrator can pin messages; applicable to groups only.
    public boolean can_pin_messages;
    //True, if the administrator can add new administrators with a subset of their own privileges or demote administrators that were directly or indirectly promoted by them.
    public boolean can_promote_members;
    //True, if the administrator isn't shown in the chat member list and sends messages anonymously; applicable to supergroups only.
    public boolean is_anonymous;

    //被禁止 - chatMemberStatusBanned
    //Point in time (Unix timestamp) when the user will be unbanned; 0 if never. If the user is banned for more than 366 days or for less than 30 seconds from the current time, the user is considered to be banned forever.
    public long banned_until_date;

    //群创建者 - chatMemberStatusCreator - The user is the owner of a chat and has all the administrator privileges.
    //A custom title of the owner; 0-16 characters without emojis; applicable to supergroups only.
    //@property (nonatomic, copy) NSString *custom_title;
    //True, if the creator isn't shown in the chat member list and sends messages anonymously; applicable to supergroups only.
    //@property (nonatomic) BOOL is_anonymous;
    //True, if the user is a member of the chat.
    public boolean is_member;

    //已离开群 - chatMemberStatusLeft

    //群成员 - chatMemberStatusMember

    //受限制成员 - chatMemberStatusRestricted - The user is under certain restrictions in the chat. Not supported in basic groups and channels.
    //待扩展
    //......

    public TelegramManager.GroupMemberState getMemberState() {
        //chatMemberStatusAdministrator, chatMemberStatusBanned, chatMemberStatusCreator,
        // chatMemberStatusLeft, chatMemberStatusMember, and chatMemberStatusRestricted
        if ("chatMemberStatusAdministrator".equals(type)) {
            return TelegramManager.GroupMemberState.GroupMemberState_Administrator;
        }
        if ("chatMemberStatusBanned".equals(type)) {
            return TelegramManager.GroupMemberState.GroupMemberState_Banned;
        }
        if ("chatMemberStatusCreator".equals(type)) {
            return TelegramManager.GroupMemberState.GroupMemberState_Creator;
        }
        if ("chatMemberStatusLeft".equals(type)) {
            return TelegramManager.GroupMemberState.GroupMemberState_Left;
        }
        if ("chatMemberStatusMember".equals(type)) {
            return TelegramManager.GroupMemberState.GroupMemberState_Member;
        }
        if ("chatMemberStatusRestricted".equals(type)) {
            return TelegramManager.GroupMemberState.GroupMemberState_Restricted;
        }
        return TelegramManager.GroupMemberState.GroupMemberState_Left;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.type);
        dest.writeString(this.custom_title);
        dest.writeByte(this.can_be_edited ? (byte) 1 : (byte) 0);
        dest.writeByte(this.can_change_info ? (byte) 1 : (byte) 0);
        dest.writeByte(this.can_post_messages ? (byte) 1 : (byte) 0);
        dest.writeByte(this.can_edit_messages ? (byte) 1 : (byte) 0);
        dest.writeByte(this.can_delete_messages ? (byte) 1 : (byte) 0);
        dest.writeByte(this.can_invite_users ? (byte) 1 : (byte) 0);
        dest.writeByte(this.can_restrict_members ? (byte) 1 : (byte) 0);
        dest.writeByte(this.can_pin_messages ? (byte) 1 : (byte) 0);
        dest.writeByte(this.can_promote_members ? (byte) 1 : (byte) 0);
        dest.writeByte(this.is_anonymous ? (byte) 1 : (byte) 0);
        dest.writeLong(this.banned_until_date);
        dest.writeByte(this.is_member ? (byte) 1 : (byte) 0);
    }

    public Group_ChatMemberStatus() {
    }

    protected Group_ChatMemberStatus(Parcel in) {
        this.type = in.readString();
        this.custom_title = in.readString();
        this.can_be_edited = in.readByte() != 0;
        this.can_change_info = in.readByte() != 0;
        this.can_post_messages = in.readByte() != 0;
        this.can_edit_messages = in.readByte() != 0;
        this.can_delete_messages = in.readByte() != 0;
        this.can_invite_users = in.readByte() != 0;
        this.can_restrict_members = in.readByte() != 0;
        this.can_pin_messages = in.readByte() != 0;
        this.can_promote_members = in.readByte() != 0;
        this.is_anonymous = in.readByte() != 0;
        this.banned_until_date = in.readLong();
        this.is_member = in.readByte() != 0;
    }

    public static final Creator<Group_ChatMemberStatus> CREATOR = new Creator<Group_ChatMemberStatus>() {
        @Override
        public Group_ChatMemberStatus createFromParcel(Parcel source) {
            return new Group_ChatMemberStatus(source);
        }

        @Override
        public Group_ChatMemberStatus[] newArray(int size) {
            return new Group_ChatMemberStatus[size];
        }
    };

    public boolean isMyManageGroup(){
        switch (getMemberState()) {
            case GroupMemberState_Administrator:
                return true;
            case GroupMemberState_Creator:
                return is_member;
            default:
                return false;
        }
    }

    public boolean isMyCreatorGroup(){
        return  getMemberState() == TelegramManager.GroupMemberState.GroupMemberState_Creator&&is_member;
    }

    public boolean isMyJoinGroup(){
        if(isMyGroup()){
            if(isMyManageGroup()){
                return false;
            }else{
                return true;
            }
        }
        return false;
    }

    public boolean isMyGroup(){
        switch (getMemberState()) {
            case GroupMemberState_Administrator:
                return true;
            case GroupMemberState_Creator:
                return is_member;
            case GroupMemberState_Left:
                return false;
            case GroupMemberState_Member:
                return true;
            case GroupMemberState_Banned:
                return false;
            case GroupMemberState_Restricted:
                return is_member;
        }
        return false;
    }
}
